package io.chenglicun.modules.clc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.entity.BaseEntity;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.modules.clc.dao.ClcExamReleaseQuestionDao;
import io.chenglicun.modules.clc.dto.ClcExamReleaseQuestionDTO;
import io.chenglicun.modules.clc.entity.ClcExamReleaseQuestionEntity;
import io.chenglicun.modules.clc.service.ClcExamReleaseQuestionService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 考试发布题目
 *
 * @author HB 137664951@qq.com
 * @since 1.0 2023-10-18
 */
@Service
public class ClcExamReleaseQuestionServiceImpl extends CrudServiceImpl<ClcExamReleaseQuestionDao, ClcExamReleaseQuestionEntity, ClcExamReleaseQuestionDTO> implements ClcExamReleaseQuestionService {

    @Override
    public QueryWrapper<ClcExamReleaseQuestionEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcExamReleaseQuestionEntity> wrapper = new QueryWrapper<>();


        return wrapper;
    }

    @Override
    public List<ClcExamReleaseQuestionDTO> getListByExamReleaseId(Long releaseId) {
        List<ClcExamReleaseQuestionEntity> entities = baseDao.selectList(
                new QueryWrapper<ClcExamReleaseQuestionEntity>()
                        .lambda().eq(ClcExamReleaseQuestionEntity::getReleaseId, releaseId)
        );

        return ConvertUtils.sourceToTarget(entities, ClcExamReleaseQuestionDTO.class);
    }

    @Override
    @Transactional
    public void saveBatch(List<ClcExamReleaseQuestionDTO> dtos) {
        List<ClcExamReleaseQuestionEntity> entities = ConvertUtils.sourceToTarget(dtos, ClcExamReleaseQuestionEntity.class);

        this.insertBatch(entities);
    }

    @Override
    @Transactional
    public void deleteByReleaseIds(Long... releaseIds) {
        List<ClcExamReleaseQuestionEntity> entities = baseDao.selectList(
                new QueryWrapper<ClcExamReleaseQuestionEntity>()
                        .lambda().in(ClcExamReleaseQuestionEntity::getReleaseId, Arrays.asList(releaseIds))
        );

        Long[] ids = entities.stream().map(BaseEntity::getId).toArray(Long[]::new);
        this.delete(ids);
    }
}